package com.wolfheader.wolfgame.controller.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wolfheader.wolfgame.Message;
import com.wolfheader.wolfgame.bean.admin.RoleBean;
import com.wolfheader.wolfgame.controller.BaseController;
import com.wolfheader.wolfgame.dao.admin.PermissionDao;
import com.wolfheader.wolfgame.dao.admin.RoleDao;
import com.wolfheader.wolfgame.entity.admin.PermissionEntity;
import com.wolfheader.wolfgame.entity.admin.RoleEntity;
import com.wolfheader.wolfgame.service.admin.AdminService;
import com.wolfheader.wolfgame.service.admin.RoleService;
import com.wolfheader.wolfgame.util.JqDataTabelUtils;

@Controller
@RequestMapping("/admin/role")
public class RoleController extends BaseController {

	private static final String template_url = "admin/role";

	private static final String INDEX_REDIRECT_URL = "redirect:/admin/role";

	@Autowired
	private RoleDao roleDao;

	@Resource(name = "roleServiceImpl")
	private RoleService roleService;

	@Resource(name = "adminServiceImpl")
	private AdminService adminService;

	@Autowired
	private PermissionDao permissionDao;

	@RequestMapping(method = RequestMethod.GET)
	public String role(HttpServletRequest request, ModelMap map) {
		map.addAttribute("roles", roleDao.findAll());
		map.addAttribute("menu1", "role");
		map.addAttribute("menu2", "list");
		return RoleController.template_url + "/list";
	}

	/**
	 * 刪除角色
	 * 
	 * @param id
	 * @param request
	 * @param map
	 * @param attr
	 * @return
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String delete(@PathVariable Long id, HttpServletRequest request, ModelMap map, RedirectAttributes attr) {
		RoleEntity entity = roleDao.findOne(id);
		if (entity != null) {
			roleService.delete(id);
			addFlashMessage(attr, Message.success("删除成功", INDEX_REDIRECT_URL));
			return INDEX_REDIRECT_URL;
		}
		addFlashMessage(attr, Message.error("删除失敗"));
		return INDEX_REDIRECT_URL;
	}

	/**
	 * 添加角色-頁面
	 * 
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpServletRequest request, ModelMap map) {
		map.addAttribute("permissions", permissionDao.findAll());
		map.addAttribute("menu1", "role");
		map.addAttribute("menu2", "add");
		return template_url + "/add";
	}

	/**
	 * 添加角色-請求
	 * 
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@Validated RoleBean bean, HttpServletRequest request, RedirectAttributes attr) {
		roleService.addRole(bean);
		addFlashMessage(attr, Message.success("添加成功"));
		return INDEX_REDIRECT_URL;
	}

	/**
	 * 修改角色
	 * 
	 * @param id
	 * @param bean
	 * @param request
	 * @param attr
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String edit(@PathVariable Long id, @Validated RoleBean bean, HttpServletRequest request,
			RedirectAttributes attr) {
		System.out.println(bean.isEnabled());
		boolean result = roleService.modif(id, bean);
		if (result) {
			addFlashMessage(attr, Message.success("修改成功"));
		} else {
			addFlashMessage(attr, Message.error("修改失败"));
		}
		return INDEX_REDIRECT_URL;
	}

	
	/**
	 * 修改角色-頁面
	 * 
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Long id, RedirectAttributes attr, ModelMap map) {
		RoleEntity entity = roleDao.findOne(id);
		if (entity == null) {
			addFlashMessage(attr, Message.error("权限不存在"));
			return INDEX_REDIRECT_URL;
		}
		Set<PermissionEntity> list = entity.getPermissions();
		List<PermissionEntity> alls = new ArrayList<PermissionEntity>();
		for (PermissionEntity permission : permissionDao.findAll()) {
			if (!list.contains(permission)) {
				alls.add(permission);
			}
		}
		map.addAttribute("myList", list);
		map.addAttribute("permissions", alls);
		map.addAttribute("role", entity);
		map.addAttribute("menu1", "role");
		map.addAttribute("menu2", "list");
		return template_url + "/edit";
	}

	@RequestMapping(value = "/list.json", method = RequestMethod.POST)
	public @ResponseBody String list(HttpServletRequest request, @RequestParam String sEcho,
			@RequestParam Integer iDisplayStart, @RequestParam Integer iDisplayLength,
			@RequestParam final String sSearch) {

		Sort sort = new Sort(Direction.DESC, "id");
		Page<RoleEntity> page = roleDao.findAll(new PageRequest(iDisplayStart, iDisplayLength, sort));
		return JqDataTabelUtils.getDataTabelJson(page, iDisplayStart, sEcho);
	}

}
